﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using TrainingManagement.Models;

namespace TrainingManagement.Controllers
{
    public class CourseAPIController : ApiController
    {
        private trainingDBEntities db = new trainingDBEntities();

        // GET api/CourseAPI
        public IEnumerable<tblCourse> GettblCourses()
        {
            return db.tblCourses.AsEnumerable();
        }

        // GET api/CourseAPI/5
        public tblCourse GettblCourse(int id)
        {
            tblCourse tblcourse = db.tblCourses.Single(t => t.ID == id);
            if (tblcourse == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return tblcourse;
        }

        // PUT api/CourseAPI/5
        public HttpResponseMessage PuttblCourse(int id, tblCourse tblcourse)
        {
            if (ModelState.IsValid && id == tblcourse.ID)
            {
                db.tblCourses.Attach(tblcourse);
                db.ObjectStateManager.ChangeObjectState(tblcourse, EntityState.Modified);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/CourseAPI
        public HttpResponseMessage PosttblCourse(tblCourse tblcourse)
        {
            if (ModelState.IsValid)
            {
                db.tblCourses.AddObject(tblcourse);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tblcourse);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tblcourse.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/CourseAPI/5
        public HttpResponseMessage DeletetblCourse(int id)
        {
            tblCourse tblcourse = db.tblCourses.Single(t => t.ID == id);
            if (tblcourse == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.tblCourses.DeleteObject(tblcourse);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, tblcourse);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}